package com.nidu.demo.notice.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.notice.convertor.NoticeConvertor;
import com.nidu.demo.notice.convertor.NoticeCriteriaConvertor;
import com.nidu.demo.notice.dto.NoticeCO;
import com.nidu.demo.notice.dto.NoticePageQry;
import com.nidu.demo.notice.gateway.NoticeGateway;
import com.nidu.demo.notice.model.Notice;
import com.nidu.demo.notice.model.NoticeCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 通知公告分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NoticePageQryExe {

    private final NoticeGateway gateway;
    private final NoticeConvertor convertor;
    private final NoticeCriteriaConvertor criteriaConvertor;

    public PageResponse<NoticeCO> execute(NoticePageQry qry) {
        // 1. 转换查询条件
        NoticeCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<Notice> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<NoticeCO> noticeCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(noticeCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
